// 模块化
import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '../utils/localData'
// 明确路由安装功能
Vue.use(VueRouter)

import Login from '../views/login.vue'
// 路由映射关系
import Register from '../views/Register.vue'
import User from '../views/User.vue'
import UserEdit from '../views/userEdit.vue'
import Home from '../views/home.vue'
import UserFollow from '../views/UserFollow.vue'
import UserSta from '../views/UserSta.vue'
import Usercomments from '../views/Usercomments.vue'
import Classify from '../views/classify.vue'
import Comment from '../views/Comment.vue'
import Article from '../views/Article.vue'
import Search from '../views/Search.vue'

const routes = [
    {path: '/',component: Home},
    {path: '/login',component: Login,name:'Login'},
    {path: '/register',component: Register},
    // 动态路由:/id
    {path: '/article/:id',component: Article},
    {path: '/comment/:id',component: Comment},
    {path: '/classify',component: Classify},
    {path: '/search',component: Search},
    {meta: { check:true },path: '/user',component: User},
    {meta: { check:true },path: '/user_edit',component: UserEdit},
    {meta: { check:true },path: '/user_follow',component: UserFollow},
    {meta: { check:true },path: '/usersta',component: UserSta},
    {meta: { check:true },path: '/user_comments',component: Usercomments},

]

// 创建路由模块和配置
const router = new VueRouter({
    // 使用router进行路由配置
   routes,
   mode:'history'
})
// 路由守卫
// to =目标路由对象
// from = 来自那里
// next = 是否放行
router.beforeEach((to,from,next) => {
    // const PathArr = ['/user','/user_edit']
    // 部分页面访问的时候需要验证是否带有token
    // if(PathArr.indexOf(to.path) !== -1) {
        if(to.meta.check) {
        if(getToken()) {
        //    放行
            next()
        } else {
            next({path:'/login'})
        }
        // 普通页面，直接放行
    }else {
        next()
    }
})
// 暴露出去
export default router